package cn.wolfcode.trip.web.controller;

import cn.wolfcode.trip.base.domain.Hotel;
import cn.wolfcode.trip.base.mapper.HotelDictionaryItemMapper;
import cn.wolfcode.trip.base.mapper.RegionMapper;
import cn.wolfcode.trip.base.query.HotelQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IHotelService;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.QiniuCloudUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/hotel")
public class HotelController {
    @Autowired
    private IHotelService hotelService;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private HotelDictionaryItemMapper hotelDictionaryItemMapper;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") HotelQueryObject qo){
        model.addAttribute("result",hotelService.query(qo));
        model.addAttribute("cities",regionMapper.selectByParentId());
        model.addAttribute("businesses",hotelDictionaryItemMapper.selectByParentId(1L));
        model.addAttribute("traffics",hotelDictionaryItemMapper.selectByParentId(3L));
        return "hotel/list";
    }

    @RequestMapping("/listDisable")
    public String listDisable(Model model, @ModelAttribute("qo") HotelQueryObject qo){
        qo.setState(0);
        model.addAttribute("result",hotelService.query(qo));
        return "hotel/listDisable";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id){
        JSONResult result = new JSONResult();

        try {
            hotelService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败");
        }

        return result;
    }




    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Hotel hotel, MultipartFile img){
        if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
            String url = QiniuCloudUtil.uploadQiniu(img);
            hotel.setCoverUrl(url);
        }

        hotelService.saveOrUpdate(hotel);

        return "redirect:/hotel/list.do";
    }


}
